package com.example.demo.controller;

import com.example.demo.dao.entity.*;
import com.example.demo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/bookTime")
public class BookTimeController {
    @Autowired
    private BookTimeService bookTimeService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private SysUserService sysUserService;
    /**
     * 查询预约列表
     * @param model
     * @param bookTime
     * @return
     */
    @GetMapping("/list")
    public String getBookTimeList(Model model, BookTime bookTime) {
        List<BookTime> list = bookTimeService.lambdaQuery().eq(BookTime::getStatus,0).list();
        for (BookTime time : list) {
            Doctor doctor = doctorService.getById(time.getDoctorId());
            time.setDoctorName(doctor.getName());
            Department department = departmentService.getById(doctor.getDepartmentId());
            time.setDepartmentName(department.getName());
            Hospital hospital = hospitalService.getById(department.getHospitalId());
            time.setHospitalName(hospital.getHospitalName());
            SysUser user = sysUserService.getById(time.getUserId());
            time.setUserName(user.getRealName());
        }
        model.addAttribute("list", list);
        return "bookTime/list";
    }

    /**
     * 新增预约页面
     * @param model
     * @param bookTime
     * @param id
     * @return
     */
    @GetMapping("addpage")
    public String addpage(Model model, @ModelAttribute BookTime bookTime, @RequestParam(value = "id",required = false) Integer id){
        if(!StringUtils.isEmpty(id)){
            BookTime byId = bookTimeService.getById(id);
            model.addAttribute("bookTime",byId);
        }
        List<Hospital> list = hospitalService.list();
        model.addAttribute("hospitals",list);
        return "bookTime/add";
    }

    /**
     * 新增预约
     * @param model
     * @param bookTime
     * @param principal
     * @return
     */
    @PostMapping(value = {"/add", "/update"}, produces = "application/json")
    public String saveOrUpdate(Model model, BookTime bookTime, Principal principal) {
        String loginName = principal.getName();
        Date now = new Date();
        bookTime.setCreatePerson(loginName);
        bookTime.setUpdatePerson(loginName);
        if(bookTime.getId() == null){
            bookTime.setCreateTime(now);
        }
        SysUser one = sysUserService.lambdaQuery().eq(SysUser::getLoginName, loginName).one();
        bookTime.setUserId(one.getId());
        bookTime.setUpdateTime(now);
        bookTimeService.saveOrUpdate(bookTime);
        return getBookTimeList(model,bookTime);
    }


    /**
     * 删除预约
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public String deleteBookTimeById( @PathVariable Long id) {
        bookTimeService.lambdaUpdate().eq(BookTime::getId,id).set(BookTime::getStatus,1).update();
        return "success";
    }
}
